<?php
/**
 * @author gao xiang from livesdrop.com
 * @copyright 2009
 * @uses 关于数据库的类
 */
/* 数据库类 */
class MYSQL {
	private $db_host,$db_user,$db_pass,$db_database;
	public $arr_config_r;
	/* 构造函数 */
	public function __construct($db_host = 'localhost',$db_user = 'klniu',$db_pass= '68487256',$db_database = 'klniu')
	{
		$this->arr_config_r = $arr_config_r;
		$this->db_host = $db_host;
		$this->db_user = $db_user;
		$this->db_pass = $db_pass;
		$this->db_database = $db_database;
		$this->connect_db();
	}
	/* 获得配置 */
	public function get_config(){
		$param_num = func_num_args();
		if ($param_num < 1){
			echo "ERROR:Please give one param at least!";
		}
		/* 参数连接获取查询 */
		for ($i = 0;$i < $param_num;++$i){
			$arr_param = func_get_arg($i);
			$param .= ' or config_field= "'. $arr_param . '"';
		}
		$param_1 = strstr($param,"config_field");
		$result_1=mysql_query("select config_value from config where ($param_1)order by config_id");
		$i = 0;
		while($arr_config = mysql_fetch_assoc($result_1)){
			$this->arr_config_r[$i] = $arr_config[config_value];
			++$i;
		}
		
	}
	/*连接数据库*/
	public function connect_db()
	{
		$link_to_sql = mysql_connect($this->db_host,$this->db_user,$this->db_pass)
				or die ('Not connected : ' . mysql_error());
		mysql_select_db($this->db_database, $link_to_sql) or die ('Can\'t use klniu : ' . mysql_error());
		mysql_query("set names utf8",$link_to_sql);
	}
	
	/*获取文章(多个或单个)*/	
	public function fetch_posts($author = 0, $post_id = 0, $post_s_date = NULL,$post_e_date = NULL,$post_category = 1)
	{
		global $db_config;
		if ($post_id){
			$query = "select * from post where post_id = $post_id";	
		}
		elseif($author){
			$query = "select * from post where post_author=$author order by post_id desc";
		} 
		elseif($post_s_date != NULL){
			$query = 'select * from post where (post_date >= "' . $post_s_date . '" and post_date < "'. $post_e_date . '") order by post_id desc';
		}elseif($post_category){
			$query = "select * from post where post_category = $post_category order by post_id desc";
		}
		else {
			$query = "select * from post order by post_id desc";
		}		
		$all_result = mysql_query($query) or die ("Invalid query");
		while ($allnum = mysql_fetch_assoc($all_result))
		{
			?>
			<div id="page">
				<div class="title">
				<a href="<?php echo $database->arr_config_r[4];//网站地址?>action.php?action=singlepost&postid=<?php echo $allnum[post_id];//文章链接?>"><?php echo $allnum[post_title];//网站标题?></a>
				</div>
				<div class="info">
				Composed by <a href="<?php echo $database->arr_config_r[4];//网站地址?> action.php?action=getuser&userid=<?php echo $allnum[post_author];//作者 此段为作者所有文章链接?>"><?php echo $this->user_converter($allnum[post_author]);?></a>
				&nbsp;&nbsp;&nbsp;&nbsp;
				<?php echo $allnum[post_date];//文章日期?>
				&nbsp;&nbsp;&nbsp;&nbsp;
				分类：<a href="<?php echo $database->arr_config_r[4];//网站地址?>action.php?action=getcategory&categoryid=<?php echo $allnum[post_category];//文章分类?>"><?php $category=$this->category_converter($allnum[post_category]); echo $category[0]; //此段为显示分类下的所有文章?></a>
				</div>
				<div class="content"><?php echo $allnum[post_content];?></div>
			</div>
<?php						
		}
		
	}
	
	/* 作者id转名字 */
	public function user_converter($user_id){
		$name = mysql_fetch_row(mysql_query("select name from user where id=$user_id")) or die ("Invalid query");
		return $name[0];	
	}
	
	/* 获取分类 */
	public function category_converter($category_id){
		$name = mysql_fetch_row(mysql_query("select name,alias from category where id=$category_id ")) or die ("Invalid query");
		return $name;
	}
	/*析构函数*/
	public function __destruct() 
	{
		if (!($link_to_sql)){
            mysql_close();
		}	
	}
}
/* 新建对象 */
$database = new MYSQL($db_config[host],$db_config[user],$db_config[pass],$db_config[database]);
?>